<template>
    <div class="notifications-page" :class="theme">
      <!-- 页面标题和操作栏 -->
      <div class="page-header">
        <h1><i class="icon-bell"></i> 系统通知</h1>
        <div class="actions">
          <button class="action-btn" @click="markAllAsRead" v-if="notifications.length > 0">
            <i class="icon-check-all"></i> 全部已读
          </button>
          <button class="action-btn" @click="clearAll" v-if="notifications.length > 0">
            <i class="icon-trash"></i> 清空通知
          </button>
          <div class="theme-toggle">
            <button @click="setTheme('light')" :class="{ active: theme === 'light' }" title="明亮模式">
              <i class="icon-sun"></i>
            </button>
            <button @click="setTheme('dark')" :class="{ active: theme === 'dark' }" title="暗黑模式">
              <i class="icon-moon"></i>
            </button>
          </div>
        </div>
      </div>
  
      <!-- 通知筛选栏 -->
      <div class="notification-filter">
        <button 
          v-for="filter in filters" 
          :key="filter.type" 
          :class="{ active: activeFilter === filter.type }"
          @click="activeFilter = filter.type"
        >
          <i :class="filter.icon"></i>
          {{ filter.label }}
          <span class="badge" v-if="filter.count > 0">{{ filter.count }}</span>
        </button>
      </div>
  
      <!-- 通知列表 -->
      <div class="notification-list">
        <div 
          class="notification-item" 
          v-for="(notification) in filteredNotifications" 
          :key="notification.id"
          :class="[
            notification.type,
            { unread: !notification.read },
            { pinned: notification.pinned }
          ]"
        >
          <div class="notification-icon">
            <i :class="getNotificationIcon(notification.type)"></i>
          </div>
          <div class="notification-content">
            <h3>{{ notification.title }}</h3>
            <p>{{ notification.message }}</p>
            <div class="meta">
              <span class="time">{{ formatTime(notification.time) }}</span>
              <span class="tag" v-if="notification.tag">{{ notification.tag }}</span>
            </div>
          </div>
          <div class="notification-actions">
            <button 
              class="icon-btn" 
              @click="togglePin(notification.id)"
              :title="notification.pinned ? '取消置顶' : '置顶'"
            >
              <i :class="notification.pinned ? 'icon-pinned' : 'icon-pin'"></i>
            </button>
            <button 
              class="icon-btn" 
              @click="toggleRead(notification.id)"
              :title="notification.read ? '标记为未读' : '标记为已读'"
            >
              <i :class="notification.read ? 'icon-mail-open' : 'icon-mail'"></i>
            </button>
            <button class="icon-btn" @click="deleteNotification(notification.id)" title="删除">
              <i class="icon-trash"></i>
            </button>
          </div>
        </div>
  
        <!-- 空状态 -->
        <div class="empty-state" v-if="filteredNotifications.length === 0">
          <div class="empty-icon">
            <i class="icon-bell-off"></i>
          </div>
          <h3>暂无通知</h3>
          <p>当有新通知时，它们会显示在这里</p>
        </div>
      </div>
  
      <!-- 分页控件 -->
      <div class="pagination" v-if="filteredNotifications.length > 0">
        <button class="page-btn" :disabled="currentPage === 1" @click="currentPage--">
          <i class="icon-chevron-left"></i>
        </button>
        <span class="page-info">第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
        <button class="page-btn" :disabled="currentPage === totalPages" @click="currentPage++">
          <i class="icon-chevron-right"></i>
        </button>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, computed, inject } from "vue";
  
  // 主题管理
  const theme = inject("theme");
  const setTheme = (newTheme) => {
    theme.value = newTheme;
  };
  
  // 通知数据
  const notifications = ref([
    {
      id: 1,
      type: "system",
      title: "系统更新通知",
      message: "记忆单词网站已更新至v2.0.0版本，新增了单词挑战功能",
      time: new Date(Date.now() - 3600000), // 1小时前
      read: false,
      pinned: true,
      tag: "重要"
    },
    {
      id: 2,
      type: "learning",
      title: "学习进度提醒",
      message: "您今天已经学习了25个新单词，继续保持！",
      time: new Date(Date.now() - 86400000), // 1天前
      read: true,
      pinned: false
    },
    {
      id: 3,
      type: "review",
      title: "复习提醒",
      message: "您有15个单词需要今天复习，点击开始复习",
      time: new Date(Date.now() - 172800000), // 2天前
      read: false,
      pinned: false
    },
    {
      id: 4,
      type: "achievement",
      title: "成就解锁",
      message: "恭喜您获得了'单词达人'成就！",
      time: new Date(Date.now() - 259200000), // 3天前
      read: true,
      pinned: false,
      tag: "成就"
    },
    {
      id: 5,
      type: "error",
      title: "同步失败",
      message: "您的学习数据同步失败，请检查网络连接后重试",
      time: new Date(Date.now() - 345600000), // 4天前
      read: false,
      pinned: false
    }
  ]);
  
  // 分页
  const itemsPerPage = 5;
  const currentPage = ref(1);
  
  // 筛选
  const activeFilter = ref("all");
  const filters = computed(() => [
    { type: "all", label: "全部", icon: "icon-list", count: notifications.value.length },
    { type: "unread", label: "未读", icon: "icon-mail", count: notifications.value.filter(n => !n.read).length },
    { type: "pinned", label: "置顶", icon: "icon-pin", count: notifications.value.filter(n => n.pinned).length },
    { type: "system", label: "系统", icon: "icon-info", count: notifications.value.filter(n => n.type === "system").length },
    { type: "learning", label: "学习", icon: "icon-book", count: notifications.value.filter(n => n.type === "learning").length }
  ]);
  
  // 获取筛选后的通知
  const filteredNotifications = computed(() => {
    let filtered = [...notifications.value];
    
    // 应用筛选
    if (activeFilter.value === "unread") {
      filtered = filtered.filter(n => !n.read);
    } else if (activeFilter.value === "pinned") {
      filtered = filtered.filter(n => n.pinned);
    } else if (activeFilter.value !== "all") {
      filtered = filtered.filter(n => n.type === activeFilter.value);
    }
    
    // 排序：置顶的在前，然后按时间降序
    filtered.sort((a, b) => {
      if (a.pinned !== b.pinned) return a.pinned ? -1 : 1;
      return b.time - a.time;
    });
    
    return filtered;
  });
  
  // 分页计算
  const paginatedNotifications = computed(() => {
    const start = (currentPage.value - 1) * itemsPerPage;
    return filteredNotifications.value.slice(start, start + itemsPerPage);
  });
  
  const totalPages = computed(() => {
    return Math.ceil(filteredNotifications.value.length / itemsPerPage);
  });
  
  // 获取通知图标
  const getNotificationIcon = (type) => {
    const icons = {
      system: "icon-info",
      learning: "icon-book",
      review: "icon-refresh",
      achievement: "icon-award",
      error: "icon-alert"
    };
    return icons[type] || "icon-bell";
  };
  
  // 格式化时间
  const formatTime = (date) => {
    const now = new Date();
    const diff = now - date;
    
    const minute = 60 * 1000;
    const hour = 60 * minute;
    const day = 24 * hour;
    const week = 7 * day;
    
    if (diff < minute) return "刚刚";
    if (diff < hour) return `${Math.floor(diff / minute)}分钟前`;
    if (diff < day) return `${Math.floor(diff / hour)}小时前`;
    if (diff < week) return `${Math.floor(diff / day)}天前`;
    return date.toLocaleDateString();
  };
  
  // 操作函数
  const toggleRead = (id) => {
    const notification = notifications.value.find(n => n.id === id);
    if (notification) notification.read = !notification.read;
  };
  
  const togglePin = (id) => {
    const notification = notifications.value.find(n => n.id === id);
    if (notification) notification.pinned = !notification.pinned;
  };
  
  const deleteNotification = (id) => {
    notifications.value = notifications.value.filter(n => n.id !== id);
  };
  
  const markAllAsRead = () => {
    notifications.value.forEach(n => n.read = true);
  };
  
  const clearAll = () => {
    notifications.value = [];
  };
  </script>
  
  <style scoped>
  .notifications-page {
    --bg-color: #ffffff;
    --text-color: #333333;
    --primary-color: #4361ee;
    --secondary-color: #6c757d;
    --success-color: #28a745;
    --warning-color: #ffc107;
    --danger-color: #dc3545;
    --info-color: #17a2b8;
    --border-color: #e9ecef;
    --card-bg: #f8f9fa;
    --hover-bg: #f1f3f5;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    --radius: 8px;
    --spacing: 16px;
    /* max-width: 1200px; */
    margin: 0 auto;
    /* padding: var(--spacing); */
    min-height: 100vh;
    transition: all 0.3s ease;
    width: 96%;
    padding-left: 2%;
    padding-right: 2%;
  }
  
  .notifications-page.dark {
    --bg-color: #1e1e1e;
    --text-color: #f8f9fa;
    --primary-color: #5a7dff;
    --secondary-color: #868e96;
    --card-bg: #2d2d2d;
    --border-color: #3d3d3d;
    --hover-bg: #343434;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }
  
  .notifications-page {
    background-color: var(--bg-color);
    color: var(--text-color);
  }
  
  /* 页面标题 */
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--spacing);
    padding-bottom: var(--spacing);
    border-bottom: 1px solid var(--border-color);
    padding-top: 3vh;
  }
  
  .page-header h1 {
    font-size: 1.8rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0;
  }
  
  /* 操作按钮 */
  .actions {
    display: flex;
    gap: 10px;
    align-items: center;
  }
  
  .action-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s ease;
  }
  
  .action-btn:hover {
    background-color: var(--hover-bg);
  }
  
  .theme-toggle {
    display: flex;
    gap: 5px;
    margin-left: 10px;
  }
  
  .theme-toggle button {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: none;
    background: var(--card-bg);
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s ease;
  }
  
  .theme-toggle button:hover {
    background: var(--hover-bg);
  }
  
  .theme-toggle button.active {
    background: var(--primary-color);
    color: white;
  }
  
  /* 筛选栏 */
  .notification-filter {
    display: flex;
    gap: 8px;
    margin-bottom: var(--spacing);
    flex-wrap: wrap;
  }
  
  .notification-filter button {
    padding: 8px 16px;
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
    background-color: var(--card-bg);
    color: var(--text-color);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s ease;
  }
  
  .notification-filter button:hover {
    background-color: var(--hover-bg);
  }
  
  .notification-filter button.active {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
  }
  
  .badge {
    background-color: var(--danger-color);
    color: white;
    border-radius: 10px;
    padding: 2px 6px;
    font-size: 0.75rem;
    min-width: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  
  /* 通知列表 */
  .notification-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }
  
  .notification-item {
    display: flex;
    padding: var(--spacing);
    background-color: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    transition: all 0.2s ease;
    position: relative;
    border-left: 4px solid transparent;
  }
  
  .notification-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  }
  
  .notification-item.unread {
    border-left-color: var(--primary-color);
    background-color: var(--hover-bg);
  }
  
  .notification-item.pinned {
    border-left-color: var(--warning-color);
  }
  
  .notification-item.system {
    border-left-color: var(--info-color);
  }
  
  .notification-item.learning {
    border-left-color: var(--success-color);
  }
  
  .notification-item.review {
    border-left-color: var(--warning-color);
  }
  
  .notification-item.achievement {
    border-left-color: var(--success-color);
  }
  
  .notification-item.error {
    border-left-color: var(--danger-color);
  }
  
  .notification-icon {
    font-size: 1.5rem;
    margin-right: var(--spacing);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: rgba(67, 97, 238, 0.1);
    color: var(--primary-color);
  }
  
  .notification-item.system .notification-icon {
    background-color: rgba(23, 162, 184, 0.1);
    color: var(--info-color);
  }
  
  .notification-item.learning .notification-icon {
    background-color: rgba(40, 167, 69, 0.1);
    color: var(--success-color);
  }
  
  .notification-item.review .notification-icon {
    background-color: rgba(255, 193, 7, 0.1);
    color: var(--warning-color);
  }
  
  .notification-item.achievement .notification-icon {
    background-color: rgba(40, 167, 69, 0.1);
    color: var(--success-color);
  }
  
  .notification-item.error .notification-icon {
    background-color: rgba(220, 53, 69, 0.1);
    color: var(--danger-color);
  }
  
  .notification-content {
    flex-grow: 1;
    min-width: 0;
  }
  
  .notification-content h3 {
    margin: 0 0 8px 0;
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .notification-content p {
    margin: 0 0 8px 0;
    color: var(--text-color);
    opacity: 0.9;
    line-height: 1.5;
  }
  
  .meta {
    display: flex;
    gap: 12px;
    align-items: center;
    font-size: 0.85rem;
    color: var(--secondary-color);
  }
  
  .tag {
    padding: 2px 8px;
    border-radius: 4px;
    background-color: var(--primary-color);
    color: white;
    font-size: 0.75rem;
  }
  
  .notification-actions {
    display: flex;
    gap: 8px;
    margin-left: var(--spacing);
    align-self: center;
  }
  
  .icon-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--secondary-color);
    cursor: pointer;
    transition: all 0.2s ease;
  }
  
  .icon-btn:hover {
    background: var(--hover-bg);
    color: var(--primary-color);
  }
  
  /* 空状态 */
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 0;
    text-align: center;
  }
  
  .empty-icon {
    font-size: 3rem;
    color: var(--secondary-color);
    opacity: 0.5;
    margin-bottom: 16px;
  }
  
  .empty-state h3 {
    margin: 0 0 8px 0;
    font-size: 1.2rem;
    color: var(--text-color);
  }
  
  .empty-state p {
    margin: 0;
    color: var(--secondary-color);
  }
  
  /* 分页 */
  .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
    margin-top: var(--spacing);
    padding-top: var(--spacing);
    border-top: 1px solid var(--border-color);
  }
  
  .page-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
    background-color: var(--card-bg);
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s ease;
  }
  
  .page-btn:hover:not(:disabled) {
    background-color: var(--hover-bg);
  }
  
  .page-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
  
  .page-info {
    font-size: 0.9rem;
    color: var(--secondary-color);
  }
  
  /* 图标样式 */
  [class^="icon-"], [class*=" icon-"] {
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
  }
  
  .icon-bell:before { content: "🔔"; }
  .icon-sun:before { content: "☀️"; }
  .icon-moon:before { content: "🌙"; }
  .icon-info:before { content: "ℹ️"; }
  .icon-book:before { content: "📖"; }
  .icon-refresh:before { content: "🔄"; }
  .icon-award:before { content: "🏆"; }
  .icon-alert:before { content: "⚠️"; }
  .icon-check-all:before { content: "✓✓"; }
  .icon-trash:before { content: "🗑️"; }
  .icon-pin:before { content: "📌"; }
  .icon-pinned:before { content: "📍"; }
  .icon-mail:before { content: "✉️"; }
  .icon-mail-open:before { content: "📨"; }
  .icon-bell-off:before { content: "🔕"; }
  .icon-chevron-left:before { content: "◀"; }
  .icon-chevron-right:before { content: "▶"; }
  .icon-list:before { content: "☰"; }
  </style>